package com.yuan.department.dao.impl;

import com.yuan.department.dao.IEmpDao;
import com.yuan.department.entity.Emp;
import com.yuan.department.utils.DruidPoolUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @program: Train
 * @description:
 * @author: yuan_boss
 * @create: 2022-11-23 17:15
 **/
public class EmpDaoImpl implements IEmpDao {
    QueryRunner qr = new QueryRunner(DruidPoolUtil.dataSource);
    @Override
    public Integer insert(Emp emp) throws SQLException {
        String sql = "insert into t_emp(ename,sal,comm,hiredate,mgr,deptno,job) values(?,?,?,?,?,?,?)";
        return qr.update(sql,emp.getEname(),emp.getSal(),emp.getComm(),emp.getHiredate(),emp.getMgr(),emp.getDeptno(),emp.getJob());
    }

    @Override
    public Emp selectById(Integer id) throws SQLException {
        String sql = "select * from t_emp where empno = ?";
        Emp query = qr.query(sql, new BeanHandler<>(Emp.class), id);
        return query;
    }

    @Override
    public Integer updateById(Emp emp) throws SQLException {
        String sql = "update t_emp set ename = ?,sal = ?,comm = ?,hiredate = ?,mgr = ?,deptno= ?,job = ? where empno = ?";
        int update = qr.update(sql, emp.getEname(), emp.getSal(), emp.getComm(),
                emp.getHiredate(), emp.getMgr(), emp.getDeptno(), emp.getJob(), emp.getEmpno());
        return update ;
    }

    @Override
    public Integer deleteById(Integer id) throws SQLException {
        String sql = "delete from t_emp where empno = ?";
        return qr.update(sql,id);
    }

    @Override
    public List<Emp> selectList() throws SQLException {
        String sql ="select * from t_emp";
        List<Emp> query = qr.query(sql, new BeanListHandler<>(Emp.class));
        return query;
    }
}
